Test Failed
Push — master ( 853a48...344ee9 )
by Emil
02:45
created

deliveries.getDeliveries   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 14
rs 9.95
c 0
b 0
f 0
1
const db = require("../db/database.js");
2
3
const deliveries = {
4
    sql: "SELECT ROWID as id, productId as product_id, amount," +
5
                    " deliveryDate as delivery_date, comment" +
6
                    " FROM deliveries WHERE apiKey = ?",
7
8
    getDeliveries: function(res, apiKey) {
9
        db.all(deliveries.sql, apiKey, (err, rows) => {
10
            if (err) {
11
                return res.status(500).json({
12
                    errors: {
13
                        status: 500,
14
                        source: "/deliveries",
15
                        title: "Database error",
16
                        detail: err.message
17
                    }
18
                });
19
            }
20
21
            res.json({ data: rows });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
22
        });
23
    },
24
25
    getDelivery: function(res, deliveryId, apiKey, status=200) {
26
        db.get(
27
            deliveries.sql + " AND ROWID = ?",
28
            apiKey,
29
            deliveryId,
30
            function(err, row) {
31
                if (err) {
32
                    return res.status(500).json({
33
                        errors: {
34
                            status: 500,
35
                            source: "/deliveries",
36
                            title: "Database error",
37
                            detail: err.message
38
                        }
39
                    });
40
                }
41
42
                res.status(status).json({ data: row });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
43
            });
44
    },
45
46
    addDelivery: function(res, body) {
47
        const sql = "INSERT INTO deliveries (productId, amount, deliveryDate," +
48
                        " comment, apiKey) VALUES (?, ?, ?, ?, ?)";
49
50
        db.run(sql,
51
            body.product_id,
52
            body.amount,
53
            body.delivery_date,
54
            body.comment,
55
            body.api_key,
56
            function(err) {
57
                if (err) {
58
                    return res.status(500).json({
59
                        errors: {
60
                            status: 500,
61
                            source: "/delivery",
62
                            title: "Database error",
63
                            detail: err.message
64
                        }
65
                    });
66
                }
67
68
                deliveries.getDelivery(res, this.lastID, body.api_key, 201);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
69
            });
70
    },
71
72
    deleteDelivery: function(res, body) {
73
        if (Number.isInteger(parseInt(body.id))) {
74
            const sql = "DELETE FROM deliveries WHERE apiKey = ? AND ROWID = ?";
75
76
            db.run(sql,
77
                body.api_key,
78
                body.id, (err) => {
79
                    if (err) {
80
                        return res.status(500).json({
81
                            errors: {
82
                                status: 500,
83
                                source: "/orders",
84
                                title: "Database error",
85
                                detail: err.message
86
                            }
87
                        });
88
                    }
89
90
                    res.status(204).send();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
91
                });
92
        } else {
93
            res.status(400).json({
94
                errors: {
95
                    status: 400,
96
                    detail: "Required attribute delivery id (id)" +
97
                        " was not included in the request."
98
                }
99
            });
100
        }
101
    }
102
};
103
104
module.exports = deliveries;
105